//
//  CCStatusRetweetedView.swift
//  CC-SinaWeibo
//
//  Created by Chenchen on 16/2/26.
//  Copyright © 2016年 Chenchen. All rights reserved.
//

import UIKit
import SnapKit

class CCStatusRetweetedView: UIView {
    
    /// 底部视图约束记录
    var bottomViewTopConstaint: Constraint?
    
    /// 添加约束对象属性,用于使用的记录
    var bottomConstaint: Constraint?
    
    var retweetedStatus: CCStatus? {
        didSet {
            // 设置数据
            retweetedLabel.text = retweetedStatus?.text
            pictureView.imageURLs = retweetedStatus?.imageURLs

            // 卸载老的约束对象
            self.bottomConstaint?.uninstall()
            // 根据数据调整关系
            if let urls = retweetedStatus?.imageURLs where urls.count != 0 {
                // 说明有配图视图, 转发微博底部相对于配图视图进行约束
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                }
            } else {
                // 说明没有配图视图,转发微博底部相对于文字进行约束
                self.snp_updateConstraints { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                }
            }
        }
    }
    
    // 重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置背景颜色
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        // 调用
        setupUI()
    }
    
    // 默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置界面
    private func setupUI() {
        // 添加
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        // 设置标签文字约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        
        // 设置配图视图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        
        // 转发微博底部约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    

    
    /// 懒加载标签文字
    private lazy var retweetedLabel: UILabel = UILabel(text: "转发微博:这是一条转发微博", fontSize: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    
    /// 懒加载配图视图
    private lazy var pictureView: CCStatusPictureView = CCStatusPictureView()
}
